package com.huhu.manager;

import org.andengine.engine.Engine;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.ui.IGameInterface.OnCreateSceneCallback;

import com.huhu.base.BaseScene;
import com.huhu.scenes.GameScene;
import com.huhu.scenes.LevelScene;
import com.huhu.scenes.LoadingScene;
import com.huhu.scenes.MainMenuScene;
import com.huhu.scenes.SplashScene;

public class SceneManager {

	private BaseScene splashScene;
	private BaseScene menuScene;
	private BaseScene levelScene;
	private BaseScene loadingScene;
	private BaseScene gameScene;
	
	private static final SceneManager INSTANCE = new SceneManager();
	private BaseScene currentScene;
	private SceneType currentSceneType = SceneType.SPLASH;
	private Engine engine = ResourcesManager.getInstance().engine;
	
	public static int maxLevel = 5;
	public static int currentLevel = 5;
	public static float savePositionX = 0;
	public static float savePositionY = 0;
	public static boolean saveActivte = false;

	public enum SceneType {
		SPLASH, MENU, LEVEL, LOADING, GAME
	}

	public void setScene(BaseScene scene) {
		engine.setScene(scene);
		currentScene = scene;
		currentSceneType = scene.getSceneType();
	}

	public void setScene(SceneType type) {
		switch (type) {
		case SPLASH:
			setScene(splashScene);
			break;
		case MENU:
			setScene(menuScene);
			break;
		case LEVEL:
			setScene(levelScene);
			break;
		case LOADING:
			setScene(loadingScene);
			break;
		case GAME:
			setScene(gameScene);
			break;
		default:
			break;
		}
	}
	
	public BaseScene getCurrentScene(){
		return currentScene;
	}
	
	public SceneType getCurrentSceneType(){
		return currentSceneType;
	}
	
	public void createSplashScene(OnCreateSceneCallback pOnCreateSceneCallback){
		ResourcesManager.getInstance().loadSplashTexture();
		splashScene = new SplashScene();
		currentScene = splashScene;
		
		pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
	}
	
	public void disposeSplashScene(){
		ResourcesManager.getInstance().unLoadSplashTexture();
		splashScene.disposeScene();
		splashScene = null;
	}
	
	public void createMenuScene(){
		ResourcesManager.getInstance().loadMenuResources();
		menuScene = new MainMenuScene();
		setScene(menuScene);
		disposeSplashScene();
	}
	
	public void loadMenuScene(final Engine engine){
		ResourcesManager.getInstance().unLoadLevelTextures();
		ResourcesManager.getInstance().loadMenuTextures();
		setScene(menuScene);
	}
	
	public void createLevelScene(){
		ResourcesManager.getInstance().loadLevelResources();
		levelScene = new LevelScene();
		loadingScene = new LoadingScene();
		setScene(levelScene);
		ResourcesManager.getInstance().unLoadMenuTextures();
	}
	
	public void loadLevelScene(final Engine engine){
		setScene(loadingScene);
		gameScene.disposeScene();
		ResourcesManager.getInstance().unLoadGameTextures(currentLevel);
		engine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				engine.unregisterUpdateHandler(pTimerHandler);
				ResourcesManager.getInstance().loadLevelTextures();
				levelScene = new LevelScene();
				setScene(levelScene);
			}
		}));
	}
	
	public void loadGameScene(final Engine engine, final int level){
		SceneManager.currentLevel = level;
		setScene(loadingScene);
		ResourcesManager.getInstance().unLoadLevelTextures();
		engine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				engine.unregisterUpdateHandler(pTimerHandler);
				ResourcesManager.getInstance().loadGameResources(level);
				gameScene = new GameScene();
				setScene(gameScene);
			}
		}));
	}
	
	public void replayGame(){
		gameScene = new GameScene();
		setScene(gameScene);
	}
	
	public void nextLevel(){
		SceneManager.saveActivte = false;
		if(SceneManager.currentLevel == SceneManager.maxLevel){
			SceneManager.maxLevel ++;
		}
		loadLevelScene(engine);
	}
	
	public static SceneManager getInstance(){
		return INSTANCE;
	}
}
